/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.shop.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.thinkgem.jeesite.modules.shop.entity.BusinessCategory;
import com.thinkgem.jeesite.modules.shop.service.BusinessCategoryService;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.entity.UserBusiness;
import com.thinkgem.jeesite.modules.sys.security.SystemAuthorizingRealm;
import com.thinkgem.jeesite.modules.sys.service.SystemService;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.shop.entity.BusinessProduct;
import com.thinkgem.jeesite.modules.shop.service.BusinessProductService;

import java.util.List;

/**
 * 商品信息Controller
 * @author 开发者
 * @version 2016-07-10
 */
@Controller
@RequestMapping(value = "${adminPath}/shop/businessProduct")
public class BusinessProductController extends BaseController {

	@Autowired
	private BusinessProductService businessProductService;

    @Autowired
    private  SystemService systemService;

    @Autowired
    private BusinessCategoryService businessCategoryService;
	
	@ModelAttribute
	public BusinessProduct get(@RequestParam(required=false) String id) {
		BusinessProduct entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = businessProductService.get(id);
		}
		if (entity == null){
			entity = new BusinessProduct();
		}
		return entity;
	}

    @RequiresPermissions("shop:businessProduct:view")
    @RequestMapping(value = {""})
    public String index(BusinessProduct businessProduct,Model model){
        return "modules/shop/businessProductIndex";
    }
	
	@RequiresPermissions("shop:businessProduct:view")
	@RequestMapping(value = {"list"})
	public String list(BusinessProduct businessProduct, HttpServletRequest request, HttpServletResponse response, Model model) {
        SystemAuthorizingRealm.Principal principal = UserUtils.getPrincipal();
        User user = systemService.getUser(principal.getId());
        if(user.getUserType().equals("4")){
            businessProduct.setBusiness(user);
        }else{
            businessProduct.setBusiness(null);
        }
		Page<BusinessProduct> page = businessProductService.findPage(new Page<BusinessProduct>(request, response), businessProduct); 
		model.addAttribute("page", page);
		model.addAttribute("user",user);
		return "modules/shop/businessProductList";
	}

	@RequiresPermissions("shop:businessProduct:view")
	@RequestMapping(value = "form")
	public String form(BusinessProduct businessProduct, Model model) {
        if(businessProduct.getCategory() != null &&  StringUtils.isNotBlank(businessProduct.getCategory().getId())){
            businessProduct.setCategory(businessCategoryService.get(businessProduct.getCategory().getId()));
        }
		model.addAttribute("businessProduct", businessProduct);
		return "modules/shop/businessProductForm";
	}

	@RequiresPermissions("shop:businessProduct:edit")
	@RequestMapping(value = "save")
	public String save(BusinessProduct businessProduct, Model model, RedirectAttributes redirectAttributes) {
//		if (!beanValidator(model, businessProduct)){
//			return form(businessProduct, model);
//		}
        SystemAuthorizingRealm.Principal principal = UserUtils.getPrincipal();
        User user = systemService.getUser(principal.getId());
        businessProduct.setBusiness(user);
		businessProductService.save(businessProduct);
        String businessId="";
        String categoryId = businessProduct.getCategory()!=null?businessProduct.getCategory().getId():null;
        if(user.getUserType().equals("4")){
            businessId= businessProduct.getBusiness()!=null?businessProduct.getBusiness().getId():null;
        }
		addMessage(redirectAttributes, "保存信息成功");
		return "redirect:"+Global.getAdminPath()+"/shop/businessProduct/list/?repage&category.id="+ categoryId +"&business.id="+businessId;
	}
	
	@RequiresPermissions("shop:businessProduct:edit")
	@RequestMapping(value = "delete")
	public String delete(BusinessProduct businessProduct, RedirectAttributes redirectAttributes) {
        SystemAuthorizingRealm.Principal principal = UserUtils.getPrincipal();
//        User  = new User(principal.getId());
        User user = systemService.getUser(principal.getId());
		businessProductService.delete(businessProduct);
		addMessage(redirectAttributes, "删除信息成功");
        String businessId="";
        String categoryId = businessProduct.getCategory()!=null?businessProduct.getCategory().getId():null;
        if(user.getUserType().equals("4")){
            businessId= businessProduct.getBusiness()!=null?businessProduct.getBusiness().getId():null;
        }
        return "redirect:"+Global.getAdminPath()+"/shop/businessProduct/list/?repage&category.id="+ categoryId +"&business.id="+businessId;
	}

}